package com.myutil.oss;

import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.aliyun.alidns20150109.models.DescribeDomainRecordsRequest;
import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponse;
import com.aliyun.alidns20150109.models.DescribeSubDomainRecordsRequest;
import com.aliyun.alidns20150109.models.DescribeSubDomainRecordsResponse;
import com.aliyun.alidns20150109.models.DescribeSubDomainRecordsResponseBody.DescribeSubDomainRecordsResponseBodyDomainRecordsRecord;
import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord;
import com.aliyun.alidns20150109.models.UpdateDomainRecordRequest;
import com.aliyun.alidns20150109.models.UpdateDomainRecordResponse;

public class AliyunDomainDnsService {
	 
    com.aliyun.alidns20150109.Client client = null;
	
	public AliyunDomainDnsService(String accessKey ,String secretKey){
		// 工程代码建议使用更安全的无AK方式，凭据配置方式请参见：https://help.aliyun.com/document_detail/378657.html。
		com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
		com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
				.setCredential(credential);
		// Endpoint 请参考 https://api.aliyun.com/product/cas
		config.endpoint = "cas.aliyuncs.com";
		config.setAccessKeyId(accessKey);
		config.setAccessKeySecret(secretKey);
		try {
			this.client = new com.aliyun.alidns20150109.Client(config);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	
	public List<DescribeSubDomainRecordsResponseBodyDomainRecordsRecord> describeSubDomainRecords(String subDomain) {
		try {
			DescribeSubDomainRecordsRequest req = new DescribeSubDomainRecordsRequest();
			req.setSubDomain(subDomain);
			
			DescribeSubDomainRecordsResponse res = client.describeSubDomainRecords(req);
			
			List<DescribeSubDomainRecordsResponseBodyDomainRecordsRecord> list = res.getBody().getDomainRecords().getRecord();
			
			System.out.println("describeSubDomainRecords() :\n"+JSON.toJSON(list));
			return list;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
	}
	
	 // 查询域名解析记录
	// 奇怪调用报错 Caused by: com.aliyun.tea.TeaException: code: 400, Specified parameter Version is not valid. request id: 207F6411-D0A5-58BA-87CE-6B5B9DA6D9C2
	public List<DescribeDomainRecordsResponseBodyDomainRecordsRecord> describeDomainRecords(String domain,String rrKeyWord,String type) {
		try {
			DescribeDomainRecordsRequest req = new DescribeDomainRecordsRequest();
			req.setDomainName(domain);
			/*req.setRRKeyWord(rrKeyWord);
			//req.setTypeKeyWord(domain);
			req.setType(type);*/
			
			DescribeDomainRecordsResponse res = client.describeDomainRecords(req);
			
			List<DescribeDomainRecordsResponseBodyDomainRecordsRecord> list = res.getBody().getDomainRecords().getRecord();
			
			System.out.println("describeDomainRecords() :\n"+JSON.toJSON(list));
			return list;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
	}
	
	public void updateDomainTxtRecordValue(String recordId,String rr,String type,String value) {
		updateDomainRecord(recordId,rr,"TXT",value) ;//   rr=private-oss value= 验证值
	}
	
	public void updateDomainRecord(String recordId,String rr,String type,String value) {
		try {
			UpdateDomainRecordRequest req = new UpdateDomainRecordRequest();
			req.setRecordId(recordId);//记录id
			req.setRR(rr);//主机记录。 当设置主域名解析（如 example.com）时，主机记录应填写"@"符号。
			req.setType(type);// A CNAME TXT
			req.setValue(value);
			UpdateDomainRecordResponse res =	client.updateDomainRecord(req);
			
			System.out.println("describeDomainRecords() :\n"+JSON.toJSON(res.getBody()));
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
	}     
}
